package com.rs.game.npc.combat.impl;

import java.util.HashMap;

import com.rs.game.Animation;
import com.rs.game.Entity;
import com.rs.game.Graphics;
import com.rs.game.Hit;
import com.rs.game.World;
import com.rs.game.WorldTile;
import com.rs.game.Hit.HitLook;
import com.rs.game.npc.NPC;
import com.rs.game.npc.combat.CombatScript;
import com.rs.game.npc.combat.NPCCombatDefinitions;
import com.rs.game.player.Player;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.utils.Utils;

public class Glacors extends CombatScript {
	
	@Override
	public Object[] getKeys() {
		return new Object[] { "Glacor" };
	}

	private boolean TrapsSettedUp = false;
	
	public void SpecialAttack(final NPC npc, final Entity target) {
		TrapsSettedUp = true;
        final HashMap < String, int[] > tiles = new HashMap < String, int[] > ();
        String key = target.getX() + "_" + target.getY();
        if (!tiles.containsKey(target.getX() + "_" + target.getY())) {
            tiles.put(key, new int[] {
                target.getX(), target.getY()
            });
        }
		WorldTile ICE = new WorldTile(target.getX(), target.getY(), target.getPlane());
        npc.setNextAnimation(new Animation(9955));
	    npc.setNextGraphics(new Graphics(905));
		World.sendProjectile(npc, ICE, 963, 60 , 40, 50, 30, 12 ,0);
        WorldTasksManager.schedule(new WorldTask() {
            private boolean firstCall;
            @Override
            public void run() {
                if (!firstCall) {
                    for (int[] tile: tiles.values()) {
                    	World.sendGraphics(null, new Graphics(2315), new WorldTile(tile[0], tile[1], 0));
                        if (target.getX() == tile[0] && target.getY() == tile[1]) 
                        target.applyHit(new Hit(target, (int) (target.getHitpoints() * 0.50), HitLook.REGULAR_DAMAGE));
                    }
                    firstCall = true;
                } else {
                    TrapsSettedUp = false;
                    stop();
                }
            }
        }, 3, 3);	
	}

	@Override
	public int attack(final NPC npc, final Entity target) {
		final NPCCombatDefinitions defs = npc.getCombatDefinitions();
		if (target instanceof Player) {
			if (Utils.getRandom(5) == 0) {
				if (!TrapsSettedUp) {
					SpecialAttack(npc, target);
					 return defs.getAttackDelay();
				}
			}
			  switch (Utils.getRandom(1)) {
			  case 0: 
			        if (target.withinDistance(npc, 2) && Utils.getRandom(1) == 0) {
			         npc.setNextAnimation(new Animation(9955));
			         delayHit(npc, 1, target, getMeleeHit(npc, Utils.getRandom(300)));
			         break;
              } else {
 			         npc.setNextAnimation(new Animation(9968));
 			         npc.setNextGraphics(new Graphics(905));
 				     World.sendProjectile(npc, target, 962, 41, 16, 41, 20, 16, 0);
 					 delayHit(npc, 1, target, getRangeHit(npc, getRandomMaxHit(npc, defs.getMaxHit(), -1, target)));
 			         break;
                  }
			  case 1:
			        if (target.withinDistance(npc, 2) && Utils.getRandom(1) == 0) {
			         npc.setNextAnimation(new Animation(9955));
			         delayHit(npc, 1, target, getMeleeHit(npc, Utils.getRandom(300)));
			         break;
              } else {
			        npc.setNextAnimation(new Animation(9967));
			        npc.setNextGraphics(new Graphics(902));
				    World.sendProjectile(npc, target, 634, 41, 16, 41, 20, 16, 0);
					delayHit(npc, 1, target, getMagicHit(npc, getRandomMaxHit(npc, defs.getMaxHit(), -1, target)));
			        break;
                  }
		     }
	   	}
	 return defs.getAttackDelay();
   }
}


